package com.yjd.job.repayment;

import com.alibaba.edas.schedulerX.ProcessResult;
import com.alibaba.edas.schedulerX.ScxSimpleJobContext;
import com.alibaba.edas.schedulerX.ScxSimpleJobProcessor;
import com.alibaba.fastjson.JSON;
import com.yjd.comm.base.enums.DbWREnums;
import com.yjd.comm.order.model.OrderModel;
import com.yjd.comm.order.service.IOrderService;
import com.yjd.comm.repayment.model.RepaymentDataModel;
import com.yjd.comm.repayment.service.IRepaymentDataService;
import com.yjd.comm.user.model.UserModel;
import com.yjd.comm.user.service.IUserService;
import com.yjd.comm.util.*;
import com.yjd.comm.vo.Constant;
import com.yjd.comm.vo.SmsTemplate;
import org.apache.log4j.Logger;

import javax.annotation.Resource;
import java.util.List;

/**
 * 还款提醒(提前3天提醒用户还款)
 * Created by yjd-wap on 2017/11/15.
 */
public class RepaymentReminderJob implements ScxSimpleJobProcessor {

    Logger logger = Logger.getLogger(RepaymentReminderJob.class);

    @Resource
    private IOrderService orderService;

    @Resource
    private IRepaymentDataService repaymentDataService;

    @Resource
    private IUserService userService;

    @Override
    public ProcessResult process(ScxSimpleJobContext scxSimpleJobContext) {
        try {
            int page = 1, rows = 10;
            Long currentTime = DateUtil.getCurrDateFristSecond();
            logger.info(" 还款提醒(提前3天提醒用户还款)，日期："+currentTime+"开始");

            while (true) {
                // 查找三天后需要还款的借款订单
                List<RepaymentDataModel> reminderList = (List<RepaymentDataModel>)repaymentDataService.getPageModel(
                        FrameUtil.newHashMap(
                                "page", page, "rows", rows,
                                "repayment_status", 0,
                                "estimation_repayment_time_gt", currentTime,
                                "estimation_repayment_time_le", currentTime + 259200),DbWREnums.READ).getDataList();
                if (null == reminderList || reminderList.size() == 0) {
                    break;
                }
                // 推送还款消息
                for (RepaymentDataModel repayment : reminderList) {
                    OrderModel order  = orderService.getModelById(repayment.getOrder_id(), DbWREnums.READ);
                    UserModel userModel = userService.getModelById(repayment.getUser_id(), DbWREnums.READ);

                    //发送短短信提醒
                    try {
                        String repayment_date = DateUtil.timestampToDateString(repayment.getEstimation_repayment_time(), DateUtil.PATTERN_yyyy_MM_dd);
                        SmsSenderUtils.smsSend(userModel.getMobile_number().toString(), SmsTemplate.getSmsTemplateMsg(SmsTemplate.SMS_REPAYMENT_MSG, order.getOrder_no(),
                                NumberUtil.formatNumber(repayment.getRepayment_amount(), NumberUtil.FORMAT_4), repayment_date), "");
                    } catch (Exception e) {
                        logger.error("还款报表到期短信提醒发送失败，还款信息" + JSON.toJSONString(repayment) + "错误信息:" + e);
                    }

                    //发送手机推送
                    try {
                        JpushUtil.pushMessage(
                                order.getUser_id(),
                                Constant.SysParamKeyEnum.PUSH_TPL_REPAYMENT_REMINDER,
                                NumberUtil.formatNumber(repayment.getRepayment_amount(), NumberUtil.FORMAT_4),
                                DateUtil.timestampToDateString(repayment.getEstimation_repayment_time(), DateUtil.PATTERN_yyyy_MM_dd));
                    } catch (Exception e1) {
                        logger.error("还款报表到期手机推送提醒发送失败，还款信息" + JSON.toJSONString(repayment) + "错误信息:" + e1);
                    }
                }
                page++;
            }
            logger.info(" 还款提醒(提前3天提醒用户还款)，日期："+currentTime+"结束");
        } catch (Exception e) {
            e.printStackTrace();
        }

        return new ProcessResult(true);
    }
}
